import {Injectable} from '@angular/core';
import {RequestUtil} from '../../../util/request-util';
import {Observable} from 'rxjs/Observable';
import {MsgResult} from '../../../models/msg-result';
import {API} from '../../../constants/constants';
import {DataAuditAPI} from "../data-audit-constants";

@Injectable()
export class DsconfService {

  constructor(private requestUtil: RequestUtil) {
  }

  insertDsConf(dsconf, fieldConfs): Observable<MsgResult> {
    const params = 'oriDsId=' + dsconf.oriDsId + '&oriTable=' + dsconf.oriTable + '&auditDsName=' + dsconf.auditDsName + '&isUse=' + dsconf.isUse +
      '&auditDsId=' + dsconf.auditDsId + '&auditTable=' + dsconf.auditTable + '&fieldConfs=' + fieldConfs + '&isKeepOri=' + dsconf.isKeepOri;
    return this.requestUtil.post(DataAuditAPI.insertDsConf, params);
  }

  // 删除数据源
  deleteDsConf(id): Observable<MsgResult> {
    return this.requestUtil.delete(DataAuditAPI.deleteDsConf + "/" + id + "?format=json")
  }

// 批量删除数据源
  deleteDsConfs(ids): Observable<MsgResult> {
    const params = 'ids=' + ids;
    return this.requestUtil.post(DataAuditAPI.deleteDsConfs, params)
  }

  // 修改数据源
  updateDsConf(dsconf, fieldConfs): Observable<MsgResult> {
    const params = '&oriDsId=' + dsconf.oriDsId + '&oriTable=' + dsconf.oriTable + '&auditDsName=' + dsconf.auditDsName +'&isUse=' + dsconf.isUse +
      '&auditDsId=' + dsconf.auditDsId + '&auditTable=' + dsconf.auditTable + '&fieldConfs=' + fieldConfs + '&isKeepOri=' + dsconf.isKeepOri;
    return this.requestUtil.post(DataAuditAPI.updateDsConf + "/" + dsconf.id + "?format=json", params)
  }

  // 查询全部
  getDsConfInfos(dsConf): Observable<MsgResult> {
    const params = 'oriDsId=' + dsConf.oriDsId + '&oriTable=' + dsConf.oriTable + '&auditDsName=' + dsConf.auditDsName +
      '&auditDsId=' + dsConf.auditDsId + '&auditTable=' + dsConf.auditTable + '&isKeepori=' + dsConf.isKeepori + '&isUse=' + dsConf.isUse;
    return this.requestUtil.get(DataAuditAPI.getDsConfInfos, params)
  }

  // 根据id获取数据源
  getDsConfById(id): Observable<MsgResult> {
    return this.requestUtil.get(DataAuditAPI.getDsConfByID + "/" + id + "?format=json")
  }

  //根据数据源id获取表
  getTableByDsId(id): Observable<MsgResult> {
    return this.requestUtil.get(DataAuditAPI.getTableByDsId  + "?format=json&id="+ id)
  }
}
